package com.bestvike.portal.service.impl;

import com.bestvike.commons.utils.StringUtils;
import com.bestvike.portal.dao.SysDeptDao;
import com.bestvike.portal.data.SysDept;
import com.bestvike.portal.enums.DeptStatus;
import com.bestvike.portal.service.BaseService;
import com.bestvike.portal.service.SysDeptService;
import com.bestvike.portal.support.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class SysDeptServiceImpl extends BaseService implements SysDeptService {
    @Autowired
    private SysDeptDao sysDeptDao;

    @Override
    public List<SysDept> fetchAll() {
        Example example = new Example(SysDept.class);
        example.createCriteria().andEqualTo("status", DeptStatus.NORMAL.getCode());
        return sysDeptDao.selectByExample(example);
    }

    @Override
    public List<SysDept> fetchTree() {
        List<SysDept> resultList = null;
        Example example = new Example(SysDept.class);
        example.createCriteria().andEqualTo("status", DeptStatus.NORMAL.getCode());
        example.orderBy("grade").orderBy("sn");
        List<SysDept> sysDeptList = sysDeptDao.selectByExample(example);
        // 组织children
        if (sysDeptList != null && sysDeptList.size() > 0) {
            resultList = SysDept.buildTree(sysDeptList);
        }
        return resultList;
    }

    @Override
    public SysDept create(SysDept sysDept) {
       if (StringUtils.isEmpty(sysDept.getId())) {
            sysDept.setId(StringUtils.guid());
        }
        if (StringUtils.isEmpty(sysDept.getStatus())) {
            sysDept.setStatus(DeptStatus.NORMAL.getCode());
        }
        // 判断数据库是否是已经有这个代码
        /*Example example = new Example(SysDept.class);
        example.createCriteria().andEqualTo("id", sysDept.getId());
        int cnt = sysDeptDao.selectCountByExample(example);
        if (cnt > 0) {
            throw new ServiceException("部门编号重复");
        }*/
        sysDeptDao.insert(sysDept);
        CacheUtils.createDept(sysDept);
        return sysDept;
    }

    @Override
    public int modify(SysDept sysDept) {
        int ret = sysDeptDao.updateByPrimaryKey(sysDept);
        CacheUtils.modifyDept(sysDept);
        return ret;
    }

    @Override
    public int remove(String ids) {
        SysDept sysDept = new SysDept();
        sysDept.setStatus(DeptStatus.DELETED.getCode());

        Example example = new Example(SysDept.class);
        example.createCriteria().andIn("id", Arrays.asList(ids.split(",")));
        int ret = sysDeptDao.updateByExampleSelective(sysDept, example);
        // int ret =  sysDeptDao.deleteByExample(example);
        CacheUtils.removeDept(Arrays.asList(ids.split(",")));
        return ret;
    }
}
